package com.guhus.ec.controller.seller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class FileController {

    @ResponseBody
    @RequestMapping("uploadFile")
    public File uploadFile(@RequestParam(value = "uploadFile") MultipartFile uploadFile, Model model) {
        String originalFileName = uploadFile.getOriginalFilename();    //获取原文件名
//        String newFilename = UUID.randomUUID() + "_" + originalFilename;    //使用UUID避免重名

        File file = new File("D:/Temp/ec/" + originalFileName);
        try {
            uploadFile.transferTo(file);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            model.addAttribute("msg", "上传失败");
        }
        String successFile = "http://localhost:8080/uploadFile/" + originalFileName;
        System.out.println("文件虚拟路径：" + successFile);
        model.addAttribute("msg", "上传成功");
        return file;
    }
}